package com.diandiansong.app.ui.commonlist;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.diandiansong.app.App;
import com.diandiansong.app.R;
import com.diandiansong.app.entity.CommodityKillInfo;
import com.diandiansong.app.event.SECOND;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.ui.index.GoodDetail;
import com.diandiansong.app.utils.A;
import com.diandiansong.app.widgets.AddAndMinusViewInItem;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import diandiansong.library.ui.base.FastListFragment;
import diandiansong.library.utils.CCallBack;
import diandiansong.library.utils.ImgLoader;

import static com.diandiansong.app.ui.index.IndexMain.mKillRemainTime;
import static com.diandiansong.app.ui.index.IndexMain.secToTime;

/**
 * 秒杀列表
 */
public class KillList extends BaseAct {
    public static void start(Context context) {
        Intent starter = new Intent(context, KillList.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("秒杀专场");
        addFragment(new SubFragment());
    }


    public static class SubFragment extends FastListFragment {
        //        TextView mTvLabel;
        TextView mTvKillH;
        TextView mTvKillM;
        TextView mTvKillS;

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            EventBus.getDefault().register(this);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }

        @Override
        public void initView(View rootView) {
            super.initView(rootView);
//            mTvLabel = rootView.findViewById(R.id.tV_time_label);
            mTvKillH = rootView.findViewById(R.id.tv_kh);
            mTvKillM = rootView.findViewById(R.id.tv_km);
            mTvKillS = rootView.findViewById(R.id.tv_ks);
        }

        @Override
        protected int getLayoutId() {
            return R.layout.kill_main;
        }

        @Override
        public int getItemLayoutRes() {
            return R.layout.good_item_add_minus;
        }

        @Override
        public void bindItemView(int position, @NonNull View convertView) {
            CommodityKillInfo.Data.ListInfo item = (CommodityKillInfo.Data.ListInfo) getItem(position);

            ImageView iv = findViewById(convertView, R.id.iv_logo);
            ImgLoader.display(iv, item.getImgs());

            fastText(convertView, R.id.tv_1, item.getCommodityName());
            fastText(convertView, R.id.tv_2, item.getCommoditySubtitle());
            fastText(convertView, R.id.tv_3, "￥" + item.getKillPrice() + item.getUnit());

            fastVisibility(convertView, R.id.tv_4, View.VISIBLE);
            TextView op = findViewById(convertView, R.id.tv_4);
            op.setPaintFlags(op.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            op.setText("￥" + item.getPrice() + item.getUnit());

            App.initGoodListAdd(getActivity(), item.getCommodityId(), convertView);
        }

        /**
         * 滑到底部时调用
         */
        @Override
        protected void loadMore() {
            onLoadFinishNoMore();
            //CommodityKillInfo o = IndexMain.sCommodityKillInfo;


            A.commodityKill(new CCallBack<CommodityKillInfo>() {
                @Override
                public void onSuccess(CommodityKillInfo o) throws Exception {
                    if (o != null && o.getData() != null && o.getData().getListInfo() != null) {
                        mAdapter.setData(o.getData().getListInfo());
                    } else {
                        mAdapter.clear();
                    }
                }

                @Override
                public void onFailure(int code, String msg) {
//                sCommodityKillInfo = null;
//                mLlKill.setVisibility(View.GONE);
                    toast(msg);//不显
                }
            });


        }

        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            AddAndMinusViewInItem aamvii = view.findViewById(R.id.add_minus);
            CommodityKillInfo.Data.ListInfo item = (CommodityKillInfo.Data.ListInfo) getItem(position);
            GoodDetail.start(getActivity(), item.getCommodityId());
        }


        private void update() {
            String ts = secToTime(mKillRemainTime);
            String[] tss = ts.split(":");
            mTvKillH.setText(tss[0]);
            mTvKillM.setText(tss[1]);
            mTvKillS.setText(tss[2]);

            //mTvLabel.setText("距离" + mKillEndTime + "点还剩");
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void reciveSecond(SECOND s) {
            update();
        }

    }
}